package com.education.wisdom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.pojo.*;
import com.education.wisdom.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

import static com.education.common.core.BaseController.writeJSON;


/**
 * 达人直播资源 信息操作处理
 *
 * @author ruoyi
 * @date 2019-02-16
 */
@Controller
@CrossOrigin
@Slf4j
@ResponseBody
@Api(tags = {"直播板块"})
@RequestMapping("/web/livebroadcast")
public class FullLivebroadcastController {

    @Autowired
    private ILivebroadcastService livebroadcastService;

    @Autowired
    private ICatalogLiveService catalogLiveService;

    @Autowired
    private IAnchorinformationService anchorinformationService;

    @Autowired
    private ICommentsLiveService commentsLiveService;

    @Autowired
    private ISysAppLoginService loginService;

    @Autowired
    private ILabelService labelService;

    @GetMapping("/directoryList")
    public void directoryList(HttpServletResponse response, Integer catalogId) throws IOException {
        log.info(LogUtil.LogInfo("查询直播目录", new Date()));
        List<CatalogLive> lives = null;
        if (catalogId != null) {
            lives = catalogLiveService.selectByParentId(catalogId);
            writeJSON(lives, response, "200", "success");
        } else {
            lives = catalogLiveService.selectByParentId(0);
            writeJSON(lives, response, "200", "success");
        }
    }

    @GetMapping("/erDirectory")
    public void erDirectory(HttpServletResponse response) throws IOException {
        log.info(LogUtil.LogInfo("查询直播1.2级目录", new Date()));
        List<CatalogLive> lives = catalogLiveService.selectByParentId(0);
        for (CatalogLive life : lives) {
            List<CatalogLive> lives1 = catalogLiveService.selectByParentId(life.getCatalogId());
            life.setList(lives1);
        }
        writeJSON(lives, response, "200", "success");
    }

    @GetMapping("/list")
    public void list(HttpServletResponse response, Integer catalogId, Integer count) throws IOException {
        log.info(LogUtil.LogInfo("直播首页查询专业下的资源", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("catalogId", catalogId);
        map.put("count", count);
        List<Livebroadcast> list = livebroadcastService.selectLivebroadcastByCatalogLiveCatalogId(map);
        writeJSON(list, response, "200", "success");
    }

    ///////////////////////////////////////***********************//////////////////////////////////////////
    @GetMapping("/directoryCheckResources")
    public void directoryCheckResources(HttpServletResponse response, Integer pageSize, Integer pageNum, Integer catalogId) throws IOException {
        log.info(LogUtil.LogInfo("查询二级页目录对应资源", new Date()));
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        if (catalogId != null) {
            CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(catalogId);
            if (catalogLive.getParentId() == 0) {//目录等级为一级时 查询二级目录下的课程 否则直接查询该目录下课程
                Map<String, Object> map = new HashMap<>();
                map.put("catalogId", catalogId);
//                PageHelper.startPage(pageNum, pageSize);
                List<Livebroadcast> list = livebroadcastService.selectLivebroadcastByCatalogLiveCatalogId(map);
                writeJSON(new PageInfo<>(list), response, "200", "success");
            } else {
//                PageHelper.startPage(pageNum, pageSize);
                List<Livebroadcast> list = livebroadcastService.selectByCatalogId(catalogId);
                writeJSON(new PageInfo<>(list), response, "200", "success");
            }
        } else {
//            PageHelper.startPage(pageNum, pageSize);
            List<Livebroadcast> list = livebroadcastService.selectLivebroadcastByCatalogLiveCatalogId(null);
            writeJSON(new PageInfo<>(list), response, "200", "success");
        }
    }

    @GetMapping("/details")
    public void details(HttpServletResponse response, Integer id) throws IOException {
        log.info(LogUtil.LogInfo("直播详情", new Date()));
        Livebroadcast livebroadcast = livebroadcastService.selectLivebroadcastById(id);
        livebroadcast.setBrowsing(livebroadcast.getBrowsing() + 1);
        livebroadcastService.updateLivebroadcast(livebroadcast);
        SysAppLogin appLogin = loginService.selectSysAppLoginById(livebroadcast.getUserId());
        livebroadcast.setUserIco(appLogin.getUserIco());
        //查2级
        CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(livebroadcast.getCatalogId());
        livebroadcast.setTwoJi(catalogLive.getName());
        livebroadcast.setCatalogName(catalogLive.getName());
        //查1级
        CatalogLive catalogLive1 = catalogLiveService.selectCatalogLiveById(catalogLive.getParentId());
        livebroadcast.setOneJi(catalogLive1.getName());
        List<Livebroadcast> list = livebroadcastService.selectByUserId(livebroadcast.getUserId());
        livebroadcast.setList(list);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", livebroadcast.getUserId());
        Anchorinformation anchorinformation = anchorinformationService.selectIsTake(map);
        livebroadcast.setAnchorinformation(anchorinformation);
        writeJSON(livebroadcast, response, "200", "success");
    }

    /**
     * @param isTure 是否审核通过（0审核中，1审核通过，2审核不通过）
     * @throws IOException
     */
    @GetMapping("/uploadRecord")
    public void uploadRecord(HttpServletResponse response, String isTure, String userId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("根据类型查询主播上传资源", new Date()));
        Map<String, Object> map = new HashMap<>();
//        map.put("type", type);
        map.put("userId", userId);
        map.put("isTure", isTure);
        PageHelper.startPage(pageNum, pageSize);
        List<Livebroadcast> list = livebroadcastService.selectShowListByType(map);
        writeJSON(new PageInfo<>(list), response, "200", "success");
    }

    @GetMapping("/isTake")
    public void isTake(HttpServletResponse response, String userId) throws Exception {
        log.info(LogUtil.LogInfo("查询主播是否认证", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        Anchorinformation anchorinformation = anchorinformationService.selectIsTake(map);
        if (anchorinformation != null) {
            if (anchorinformation.getIsTure().equals("0")) {
                CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(Integer.valueOf(anchorinformation.getLivetype()));
                CatalogLive catalogLive1 = catalogLiveService.selectCatalogLiveById(catalogLive.getParentId());

                anchorinformation.setLivetype(catalogLive1.getName());
                anchorinformation.setLivetype1(catalogLive.getName());

                anchorinformation.setLivetypeId(catalogLive1.getCatalogId());
                anchorinformation.setLivetype1Id(catalogLive.getCatalogId());

                writeJSON(anchorinformation, response, "202", "审核中");
            } else if (anchorinformation.getIsTure().equals("1")) {
                CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(Integer.valueOf(anchorinformation.getLivetype()));
                CatalogLive catalogLive1 = catalogLiveService.selectCatalogLiveById(catalogLive.getParentId());
                anchorinformation.setLivetype(catalogLive1.getName());
                //anchorinformation.setLivetype(catalogLive1.getCatalogId()+"");
                anchorinformation.setLivetype1(catalogLive.getName());
                //anchorinformation.setLivetype1(catalogLive.getCatalogId()+"");

                anchorinformation.setLivetypeId(catalogLive1.getCatalogId());
                anchorinformation.setLivetype1Id(catalogLive.getCatalogId());
                writeJSON(anchorinformation, response, "200", "success");
            } else if (anchorinformation.getIsTure().equals("2")) {
                CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(Integer.valueOf(anchorinformation.getLivetype()));
                CatalogLive catalogLive1 = catalogLiveService.selectCatalogLiveById(catalogLive.getParentId());
                anchorinformation.setLivetype(catalogLive1.getName());
                anchorinformation.setLivetype1(catalogLive.getName());
                writeJSON(anchorinformation, response, "203", "审核不通过");
            }
        } else {
            writeJSON("未提交资料", response, "201", "success");
        }
    }

    @GetMapping("/isTake2")
    public void isTake3(HttpServletResponse response, String userId) throws Exception {
        log.info(LogUtil.LogInfo("查询主播是否认证", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        Anchorinformation anchorinformation = anchorinformationService.selectIsTake(map);
        if (anchorinformation != null) {
            if (anchorinformation.getIsTure().equals("0")) {
                CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(Integer.valueOf(anchorinformation.getLivetype()));
                CatalogLive catalogLive1 = catalogLiveService.selectCatalogLiveById(catalogLive.getParentId());

                anchorinformation.setLivetype(catalogLive1.getName());
                anchorinformation.setLivetype1(catalogLive.getName());

                anchorinformation.setLivetypeId(catalogLive1.getCatalogId());
                anchorinformation.setLivetype1Id(catalogLive.getCatalogId());

                writeJSON(anchorinformation, response, "200", "审核中");
            } else if (anchorinformation.getIsTure().equals("1")) {
                CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(Integer.valueOf(anchorinformation.getLivetype()));
                CatalogLive catalogLive1 = catalogLiveService.selectCatalogLiveById(catalogLive.getParentId());
                anchorinformation.setLivetype(catalogLive1.getName());
                //anchorinformation.setLivetype(catalogLive1.getCatalogId()+"");
                anchorinformation.setLivetype1(catalogLive.getName());
                //anchorinformation.setLivetype1(catalogLive.getCatalogId()+"");

                anchorinformation.setLivetypeId(catalogLive1.getCatalogId());
                anchorinformation.setLivetype1Id(catalogLive.getCatalogId());
                writeJSON(anchorinformation, response, "200", "success");
            } else if (anchorinformation.getIsTure().equals("2")) {
                CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(Integer.valueOf(anchorinformation.getLivetype()));
                CatalogLive catalogLive1 = catalogLiveService.selectCatalogLiveById(catalogLive.getParentId());
                anchorinformation.setLivetype(catalogLive1.getName());
                anchorinformation.setLivetype1(catalogLive.getName());
                writeJSON(anchorinformation, response, "200", "审核不通过");
            }
        } else {
            writeJSON("未提交资料", response, "200", "操作成功");
        }
    }

    //@GetMapping("/isTake2")
    public void isTake2(HttpServletResponse response, String userId) throws Exception {
        log.info(LogUtil.LogInfo("查询主播是否认证", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        Anchorinformation anchorinformation = anchorinformationService.selectIsTake(map);
        if (anchorinformation != null) {
            CatalogLive catalogLive = catalogLiveService.selectCatalogLiveById(Integer.valueOf(anchorinformation.getLivetype()));
            CatalogLive catalogLive1 = catalogLiveService.selectCatalogLiveById(catalogLive.getParentId());
            anchorinformation.setLivetype(catalogLive1.getName());
            anchorinformation.setLivetype1(catalogLive.getName());
            writeJSON(anchorinformation, response, "200", "操作成功");
        } else {
            Anchorinformation a = new Anchorinformation();
            a.setIsTure("0");
            writeJSON(a, response, "200", "success");
        }
    }

    @PostMapping("/addAnchor")
    public void addAnchor(HttpServletResponse response, Anchorinformation anchorinformation) throws Exception {
        log.info(LogUtil.LogInfo("新增主播信息", new Date()));
        if (anchorinformation.getUserId() != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("userId", anchorinformation.getUserId());
            Anchorinformation amation = anchorinformationService.selectIsTake(map);
            if (amation != null) {
                writeJSON("申请失败", response, "201", "success");
            } else {
                anchorinformation.setCreateTime(new Date());
                anchorinformation.setIsTure("0");
                anchorinformationService.insertAnchorinformation(anchorinformation);
                Anchorinformation anchorinformation1 = anchorinformationService.selectAnchorinformationById(anchorinformation.getUserId());
                writeJSON(anchorinformation1, response, "200", "成功");
            }
        } else {
            writeJSON("信息不完善", response, "201", "success");
        }
    }

    @PostMapping("/addLive")
    public void addLive(HttpServletResponse response, Livebroadcast livebroadcast) throws Exception {
        log.info(LogUtil.LogInfo("新增达人直播资源", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("userId", livebroadcast.getUserId());
        Anchorinformation anchorinformation = anchorinformationService.selectIsTake(map);
        if (anchorinformation.getIsTure().equals("1")) {
            if (livebroadcast.getUrl() != null) {
                livebroadcast.setResourcesId(UUIDUtils.getByUUId());
                livebroadcast.setCreateTime(new Date());
                livebroadcast.setIsTure("0");
                livebroadcast.setBrowsing(0);
                livebroadcastService.insertLivebroadcast(livebroadcast);
                writeJSON("上传成功", response, "200", "成功");
            } else {
                writeJSON("信息不完善", response, "201", "成功");
            }
        } else {
            writeJSON("未通过审核不能上传资源", response, "201", "成功");
        }
    }

    @PostMapping("/updateAnchor")
    public void updateAnchor(HttpServletResponse response, Anchorinformation anchorinformation) throws Exception {
        log.info(LogUtil.LogInfo("修改主播信息", new Date()));
        anchorinformationService.updateAnchorinformation(anchorinformation);
        writeJSON("修改成功", response, "200", "成功");
    }

    @GetMapping("/search")
    public void search(HttpServletResponse response, String name, Integer pageNum, Integer pageSize) throws IOException {
        PageHelper.startPage(pageNum, pageSize);
        List<Livebroadcast> list = livebroadcastService.selectByLike(name);
        writeJSON(new PageInfo<>(list), response, "200", "success");
    }

    @PostMapping("/addComment")
    public void addComment(HttpServletResponse response, CommentsLive commentsLive) throws IOException {
        log.info(LogUtil.LogInfo("添加评论", new Date()));
        commentsLive.setPlCreatetime(new Date());
        commentsLiveService.insertCommentsLive(commentsLive);
        writeJSON("评论成功", response, "200", "success");
    }

    @GetMapping("/filter")
    public void filter(HttpServletResponse response, Integer id, String plPraise, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("根据（1：好评，2：中评，3：差评）筛选", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("plPraise", plPraise);
        PageHelper.startPage(pageNum, pageSize);
        List<CommentsLive> list = commentsLiveService.selectByCatalogIdAndPlPraise(map);
        for (CommentsLive commentsLive : list) {
            SysAppLogin appLogin = loginService.selectSysAppLoginById(commentsLive.getPlCreateuser().toString());
            commentsLive.setUserIcoUrl(appLogin.getUserIco());
            commentsLive.setUserName(appLogin.getUserAccount());
            Label label = labelService.selectLabelById(commentsLive.getPlAttribute());
            if (label != null) {
                commentsLive.setLabelName(label.getBqLabel());
            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "操作成功");
    }
}
